// ==UserScript==
// @name           Firefox_Goagent_Shura.uc.js
// @namespace      Firefox_Goagent_Shura.uc.js
// @description    在Firefox中调用goagent程序的脚本
// @author         Shura
// @include        main
// @charset        UTF-8
// @version        0.1
// @homepageURL    http://chenxuefeng.net.cn/
// @downloadURL http://git.oschina.net/shura/UserChromeJS/raw/master/Firefox_Goagent_Shura.uc.js
// @reviewURL      http://git.oschina.net/shura/UserChromeJS/
// @note 		   在Firefox中调用goagent
// @include        chrome://browser/content/browser.xul
// ==UserScript==
var shura = {
exec: function(path, args) {
            args = args || [];
            var args_t=args.slice(0);
        for (var i=0; i<args_t.length; i++) {
          args_t[i] = args_t[i].replace(/%u/g, gBrowser.currentURI.spec);
        }

        var file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);
        file.initWithPath(path);
        if (!file.exists()) {
          Cu.reportError('File Not Found: ' + path);
          return;
        }

        if (!file.isExecutable()) {
          file.launch();
        }
        else {
          var process = Cc['@mozilla.org/process/util;1'].createInstance(Ci.nsIProcess);
          process.init(file);
          process.run(false, args_t, args_t.length);
        }
      },

createBtn: function() {
    	var TARGET = null;
		var Btn = document.createElement("toolbarbutton");
		Btn.id = "shura-goagent";
		Btn.setAttribute("type", "none");
		Btn.setAttribute("class", "toolbarbutton-1 chromeclass-toolbar-additional");
		Btn.setAttribute("onclick", "shura.exec(this.path, this.args);");
		Btn.style.listStyleImage = "url()";
		Btn.setAttribute("label","shura-goagent");
		//Btn.setAttribute("oncommand","shura.exec(this.path, this.args);");
		Btn.path = Components.classes['@mozilla.org/file/directory_service;1'].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsILocalFile).path + '\\goa\\goagent.exe';
        Btn.args = '';
		Btn.setAttribute("tooltiptext","打开goagent");
		document.insertBefore(document.createProcessingInstruction('xml-stylesheet', 'type="text/css" href="data:text/css;utf-8,' + encodeURIComponent(
		'\
		#uac_statusbar_panel {\
		  -moz-appearance: none !important;\
		  border-style: none !important;\
		  border-radius: 0 !important;\
		  padding: 0 3px !important;\
		  margin: 0 !important;\
		  background: transparent !important;\
		  box-shadow: none !important;\
		  -moz-box-align: center !important;\
		  -moz-box-pack: center !important;\
		  min-width: 18px !important;\
		  min-height: 18px !important;\
		          }\
		#uac_statusbar_panel > .toolbarbutton-icon {\
			max-width: 18px !important;\
		    padding: 0 !important;\
		    margin: 0 !important;\
		}\
		#uac_statusbar_panel dropmarker{display: none !important;}\
		    ') + '"'), document.documentElement);
		Btn.style.padding = "0px 2px";
		
		var toolbar = document.getElementById("urlbar-icons");//图标显示位置，urlbar-icons  status-bar addon-bar searchbar TabsToolbar alltabs-button bookmarks-menu-button
		if (this.TARGET != null) {
			this.TARGET = document.getElementById(this.TARGET);
		}
		toolbar.insertBefore(Btn, this.TARGET);
    }
};
	shura.createBtn();